<?php
if (! defined ( 'BASEPATH' ))
    exit ( 'No direct script access allowed' );

class userLog extends Controller {
    public function __construct() {
        parent::__construct ();
        $this->_data = array ();
        $this->load->model ( 'userlog_model' );
        isController('myuser','userlog');
    }
    protected function _view($template, $data = array(), $result = false) {
        if ($data && is_array ( $data )) {
            return $this->load->view ( $template, $data, $result );
        }
        return $this->load->view ( $template, $this->_data, $result );
    }
    public function index() {
        redirect ( 'myuser/userLog/view' );
    }
    public function view() {
    	if (! isAccess ( 'myuser', 'userlog', 'view'))
            redirect (); 
        $array = $this->uri->uri_to_assoc ( 3, array ('kode', 'action', 'page','ip','dateFrom','dateTo','browser','user','module') );
        
        $srcIp = ( trim ( $this->input->post('srcIp') ) ) ?  trim ( $this->input->post('srcIp') ) : $array['ip'] ;
        $dateFrom  = ( trim ( $this->input->post('srcTglFrom') ) ) ? trim ( $this->input->post('srcTglFrom') ) : $array['dateFrom'] ;
        $dateTo  = ( trim ( $this->input->post('srcTglTo') ) ) ? trim ( $this->input->post('srcTglTo') ) : $array['dateTo'] ;
        $user  = ( trim ( $this->input->post('srcUser') ) ) ? trim ( $this->input->post('srcUser') ) : $array['user'] ;
        $module  = ( trim ( $this->input->post('srcModule') ) ) ? trim ( $this->input->post('srcModule') ) : $array['module'] ;
        
        // filter like model
        $srcIp = ( $srcIp == 'Alamat IP' ) ? '' : $srcIp ;
        $dateFrom  = ( $dateFrom == 'Dari Tanggal' ) ? '' : $dateFrom ;
        $dateTo  = ( $dateTo == 'Sampai Tanggal' ) ? '' : $dateTo ;
        $user  = ( $user == 'Pengguna' ) ? '' : $user ;
        $module  = ( $module == 'Module' ) ? '' : $module ;
        
        $FORM			= array("dateFrom"	=> $dateFrom,
						        "dateTo"	=> $dateTo,
        						"ip"  		=> $srcIp  , 
        						"user" 		=> $user   ,
						        "module"	=> $module,
        						);
       	
       	$dataPaging 	= uri_data( 4 , $FORM );
        
        
        $array['page'] = (int) $array['page'];

        $this->load->library('pagination');
        $total = $this->userlog_model->getCount( $srcIp , $dateFrom , $dateTo , $user , $module);
        if (($array['page'] + 1) > $total)
            $array['page'] = 0;
        $limit = $this->config->item('myuser_limit');
        $config = $this->config->item('paging');
        $config['base_url'] = site_url('myuser/userLog/view/'.$dataPaging['link'].'page/');
        $config['uri_segment'] = $dataPaging['urisegment'];
        $config['total_rows'] = $total;
        $config['per_page'] = $limit;
        
        $this->pagination->initialize($config);
        $this->_data['paging'] =  $this->pagination->create_links();
        
       
        $this->_data['dataSrcIp'] = ( ! empty ( $array['ip'] ) ) ? $array['ip'] : '' ;
        $this->_data['dataSrcDateFrom'] = ( ! empty ( $array['dateFrom'] ) ) ? $array['dateFrom'] : '' ;
        $this->_data['dataSrcDateTo'] = ( ! empty ( $array['dateTo'] ) ) ? $array['dateTo'] : '' ;
        $this->_data['dataUserName'] = ( ! empty ( $array['user'] ) ) ? $array['user'] : '' ;
        $this->_data['dataModule'] = ( ! empty ( $array['module'] ) ) ? $array['module'] : '' ;
        
        $dataView = $this->userlog_model->getList($limit,$array['page'], $srcIp , $dateFrom , $dateTo , $user , $module);
        $this->_data['dataView'] = $dataView;
        
        $this->_data['uri_to_assoc'] 	= $array;
        $this->_data['start'] 			= $array['page'] ;
        
        
        $listModule 					= array('logistik','myuser','personel','report','rikkes');

        
  		$dataModule = ' [ '  ;
        $var = '';
        foreach ( $listModule as $key=>$row ){
        	$var = $var . '  "' .$row.'" ,';
        }
        $dataModule = $dataModule.  $var . ' ] '  ;
        $this->_data['dataListModule'] 			= $dataModule ;

        $this->_view ( 'myuser/userLogView' );
    }
    
    
    
    public function getJsonAutoComplete(){
    	
    	$param = trim ( $this->input->post('q') ) ; 
    	
    	$alias =  trim ( $this->input->post('al') ) ; 
    	
    	$field =  trim ( $this->input->post('fi') ) ; 
    	
    	$cek = $this->userlog_model->getDataComplete( $param , $field , $alias);
    	
    	echo json_encode( $cek );
    
    }
    
    
}
